/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scenedemo.group.viewpager;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSliderProvider;

import java.util.List;

/**
 * DynamicFragmentPagerAdapter
 *
 * @since 2021-06-17
 */
public class DynamicFragmentPagerAdapter extends PageSliderProvider {
    private String[] mStrings = new String[]{"0", "1", "2", "3"};

    private List<DirectionalLayout> mFragments;

    /**
     * DynamicFragmentPagerAdapter
     *
     * @param fragments fragments
     */
    public DynamicFragmentPagerAdapter(List<DirectionalLayout> fragments) {
        this.mFragments = fragments;
    }

    /**
     * update
     *
     * @param fragments fragments
     */
    public void update(List<DirectionalLayout> fragments) {
        this.mFragments = fragments;
        notifyDataChanged();
    }

    @Override
    public String getPageTitle(int position) {
        if (mStrings != null && position < mStrings.length) {
            return mStrings[position];
        }
        return "默认栏目";
    }

    @Override
    public int getCount() {
        return mFragments == null ? 0 : mFragments.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int index) {
        componentContainer.addComponent(mFragments.get(index));
        return mFragments.get(index);
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int index, Object object) {
        componentContainer.removeComponent(mFragments.get(index));
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return false;
    }
}